package com.yun.zf.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.zf.admin.mapper.ZfHouseDescMapper;
import com.yun.zf.admin.service.ZfHouseDescService;
import com.yun.zf.common.admin.model.house.HouseDescMo;
import com.yun.zf.common.app.domain.ZfHouseDesc;
import com.yun.zf.common.mapstruct.admin.HouseMapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 房源描述表 服务实现类
 * </p>
 *
 * @author cj
 * @since 2021-03-01
 */
@Service
public class ZfHouseDescServiceImpl extends ServiceImpl<ZfHouseDescMapper, ZfHouseDesc> implements ZfHouseDescService {

    /**
     * 新增房源特色信息
     * @param
     * @return
     */
    @Override
    public int addHouseDes(HouseDescMo descMo, Long provinceCode, Long cityCode) {
        ZfHouseDesc desc = HouseMapper.INSTANCE.moToHouseDesc(descMo);
        desc.setProvinceCode(provinceCode);
        desc.setCityCode(cityCode);
        desc.setCreateTime(new Date());
        return   baseMapper.insert(desc);
    }

    /**
     * 更新房源特色信息
     * @param
     * @return
     */
    @Override
    public int motifyHouseDes(HouseDescMo descMo, Long provinceCode, Long cityCode) {
        QueryWrapper<ZfHouseDesc> houseDescQueryWrapper= new QueryWrapper<>();
        houseDescQueryWrapper.lambda()
                .eq(ZfHouseDesc::getProvinceCode,provinceCode)
                .eq(ZfHouseDesc::getCityCode,cityCode)
                .eq(ZfHouseDesc::getBasicId,descMo.getBasicId())
                .eq(ZfHouseDesc::getId,Long.parseLong(descMo.getId()));
       ZfHouseDesc desc = baseMapper.selectOne(houseDescQueryWrapper);
        BeanUtil.copyProperties(descMo,desc, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return   baseMapper.update(desc,houseDescQueryWrapper);
    }

    /**
     * 查询房源特色信息
     * @param basicId  房源id
     * @param provinceCode 省份代码
     * @param cityCode 城市代码
     * @return
     */
    @Override
    public HouseDescMo getHouseDes(Long basicId, Long provinceCode, Long cityCode) {

        return baseMapper.getDes(basicId,provinceCode,cityCode);
    }
}
